﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiaoYuStock.Model;
using XiaoYuStock.Model.Models;
using System.Data;
using System.Data.Entity;
using XiaoYuStock.Model.Request;
namespace XiaoYuStockAnalysis.BusinessLogic.Business
{
    public class DayDataBusiness
    {
        public bool AddData(DayData model)
       {
           if (model == null)
               throw new ArgumentNullException("model");

           ShareContext dbContext = new ShareContext();
           dbContext.Entry(model).State = EntityState.Added;
           return dbContext.SaveChanges() > 0;
       }

        public bool SynTodayData(DayData model)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            ShareContext dbContext = new ShareContext();
            var query = from m in dbContext.DayData
                        where m.ShareCode == model.ShareCode && m.CurrentDate == model.CurrentDate
                        && m.CurrentTime == model.CurrentTime
                        select m;
            if (query.Any())
            {
                var m = query.FirstOrDefault();
                m.CurrentDate = model.CurrentDate;
                m.CurrentPrice = model.CurrentPrice;
                m.CurrentTime = model.CurrentTime;
                m.FloatPrice = model.FloatPrice;
                m.IsBuy = model.IsBuy;
                m.TradeAmount = model.TradeAmount;
                m.TradeCount = model.TradeCount;
              
                dbContext.Entry(m).State = EntityState.Modified;
                dbContext.SaveChanges();
                return true;
            }
            dbContext.Entry(model).State = EntityState.Added;
            return dbContext.SaveChanges() > 0;
        }


        public bool SynData(DayData model)
       {
           if (model == null)
               throw new ArgumentNullException("model");

           ShareContext dbContext = new ShareContext();
           var query = from m in dbContext.DayData
                       where m.ShareCode == model.ShareCode && m.CurrentDate == model.CurrentDate
                       &&m.CurrentTime==model.CurrentTime
                       select m;
           if (query.Any())
               return false;
           dbContext.Entry(model).State = EntityState.Added;
           return dbContext.SaveChanges() > 0;
       }

        public IEnumerable<DayData> GetList(DayDataRequest request = null)
       {
           request = request ?? new DayDataRequest();

           using (var dbContext = new ShareContext())
           {
               IQueryable<DayData> query = dbContext.DayData;
               if (!string.IsNullOrWhiteSpace(request.ShareCode))
               {
                   query = query.Where(m => m.ShareCode == request.ShareCode);
               }
               if (request.IsBuy.HasValue)
               {
                   query = query.Where(m => m.IsBuy == (request.IsBuy.Value==1));
               }
               if (request.BeginDate.HasValue)
               {
                   var date=request.BeginDate.Value.Date;
                   query = query.Where(m => m.CurrentDate >= date);

               }
               if (request.EndDate.HasValue)
               {
                   var date=request.EndDate.Value.AddDays(1).Date;
                   query = query.Where(m => m.CurrentDate < date);
               }

               return query.OrderByDescending(u => u.CurrentTime).ToPagedList(request.PageIndex, request.PageSize);
           }
       }
    }
}
